// pages/index/nearby/nearby.js
const app = getApp()
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'E4XBZ-SDPKU-GLUV5-4TRS7-44HQS-DTBNQ' // 必填
});  
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listCompany: [],
    bigImg: '',
    location: '',
    district: '',
    latLng: '',
    conf: true
  },
  showBigImg: function (e) {
    this.setData({
      bigImg: e.currentTarget.dataset.src
    })
  },
  hideBigimg: function () {
    this.setData({
      bigImg: ''
    })
  },
  listData(location) {
    let _this = this;
    app.util.ajax({
      url: 'LookAround/look_around',
      data: {
        user_id: app.globalData.userInfo.user_id,
        district: _this.data.district
      },
      success: function (res) {
        _this.setData({
          listCompany: res.company,
          listenterprise: res.enterprise
        });
        for (let i = 0; i < _this.data.listCompany.length; i++) {
          let list = _this.data.listCompany[i];
          _this.getLatLng(list.province + list.city + list.district + list.address, i);
        };
      }
    })
  },
  //关注
  follow(e) {
    let _this = this;
    let user_id = app.globalData.userInfo.user_id;
    let type = e.currentTarget.dataset.type;
    let member_id = e.currentTarget.dataset.member_id;
    let status = e.currentTarget.dataset.status == '1' ? 0 : 1;
    app.util.ajax({
      url: 'index/follow',
      data: {
        user_id: user_id,
        com_state: type,
        member_id: member_id,
        state: status,
      },
      success: function (res) {
        _this.listData(_this.data.location);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!app.globalData.userInfo) {
      wx.showModal({
        content: '请前往登录',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: "/pages/user/user"
            })
          }
        }
      })
      return;
    }
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 地理坐标相关 开始
  getLocation: function () {
    let _this = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        _this.setData({
          conf: false,
          latLng: res.latitude + ',' + res.longitude
        });
        qqmapsdk.reverseGeocoder({
          location: res.latitude + ',' + res.longitude || '', // 传入坐标 latitude,longitude
          success: function(res2) {
            res2.result.address_component.district
            _this.setData({
              location: res.latitude + ',' + res.longitude,
              district: res2.result.address_component.district
            });
            _this.listData(_this.data.location);
          }
        })
      },
    })
  },
  getLatLng (address, index) {
    var _this = this;
    qqmapsdk.geocoder({
      address: address,
      success: function (res) {
        if (res.status === 0 && res.result.title !== res.result.address_components.district) {
          // 调用距离计算接口
          qqmapsdk.calculateDistance({
            from: _this.data.latLng,
            to: res.result.location.lat + ',' + res.result.location.lng,
            success: function (res2) {
              if (res2.status === 0) {
                let array = _this.data.listCompany;
                array[index].distance = res2.result.elements[0].distance;
                _this.setData({
                  listCompany: array
                });
              };
            }
          });
        };
      }
    })
  },
  // 地理坐标相关 结束
  gotoSetting() {
    wx.openSetting({
        success: (res) => {}
    })
  },
  getenterprise: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url: '/pages/index/listMsg/listMsg?type=1&id=' + id
    })
  },
})